package com.itcast.plane.modoule.company.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcast.plane.common.ConsHint;
import com.itcast.plane.common.PageIndex;
import com.itcast.plane.modoule.company.domain.Company;
import com.itcast.plane.modoule.company.dto.CompanyCreatDTO;
import com.itcast.plane.modoule.company.mapper.CompanyMapper;
import com.itcast.plane.modoule.flight.domain.Flight;
import com.itcast.plane.modoule.flight.mapper.FlightMapper;
import com.itcast.plane.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CompanyService extends ServiceImpl<CompanyMapper, Company> {

    @Resource
    private CompanyMapper companyMapper;

    @Resource
    private FlightMapper flightMapper;

    public JsonResult addCompany(CompanyCreatDTO creatDTO) {
        LambdaQueryWrapper<Company> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Company company = companyMapper.selectOne(lambdaQueryWrapper.eq(Company::getName,creatDTO.getName()));
        if (ObjectUtils.isEmpty(company)) {
            company = new Company();
            BeanUtils.copyProperties(creatDTO, company);
            int line = companyMapper.insert(company);
            if (line > 0) {
                return new JsonResult(true, ConsHint.INSERT_SUCCESS);
            } else {
                return new JsonResult(false, ConsHint.INSERT_DEFAULT);
            }
        }else {
            return new JsonResult(false,ConsHint.INSERT_DEFAULTED);
        }
    }

    public JsonResult deleteCompany(int id) {
        LambdaQueryWrapper<Flight> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        int i = flightMapper.selectCount(lambdaQueryWrapper.eq(Flight::getCompId,id));
        if (i>0){
            return new JsonResult(false,ConsHint.DELETE_DEFAULT_USING);
        }
        companyMapper.deleteById(id);
        return new JsonResult(true,ConsHint.DELETE_SUCCESS);
    }

    public JsonResult editCompany(Company company) {
        LambdaQueryWrapper<Company> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Company company1 = companyMapper.selectOne(lambdaQueryWrapper.eq(Company::getName,company.getName()));
        if (ObjectUtils.isEmpty(company1)){
            companyMapper.updateById(company);
            return new JsonResult(true,ConsHint.EDIT_SUCCESS);
        }else {
            return new JsonResult(false,ConsHint.EDIT_DEFAULT);
        }

    }

    public JsonResult getPage(int pages) {
        LambdaQueryWrapper<Company> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Company::getId).eq(Company::getIfDelete,0);
        IPage<Company> page = page(new Page<>(pages, PageIndex.pageSize),lambdaQueryWrapper);
        return new JsonResult(true,page);
    }

    public JsonResult getAll() {
        LambdaQueryWrapper<Company> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Company> list = companyMapper.selectList(lambdaQueryWrapper.eq(Company::getIfDelete,0));
        return new JsonResult(true,list);
    }
}
